package io.halo.payment.rqrs.msg;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;

import java.io.Serializable;

/**
 * 上游渠道支付响应信息包装类
 *
 * @author: austin
 * @since: 2023/1/11 15:12
 */
@Data
@NoArgsConstructor
@Slf4j
public class ChannelReturnMessage implements Serializable {

    /**
     * 上游渠道返回状态
     */
    private ChannelStateEnum channelStateEnum;

    /**
     * 渠道订单号
     */
    private String channelOrderId;

    /**
     * 渠道用户标识
     */
    private String channelUserId;

    /**
     * 渠道错误码
     */
    private String channelErrCode;

    /**
     * 渠道错误描述
     */
    private String channelErrMsg;

    /**
     * 是否需要轮询查单（比如微信条码支付） 默认不查询订单
     **/
    private boolean isNeedQuery = false;

    /**
     * 响应结果（一般用于会地哦啊接口返回给上游数据）
     */
    private ResponseEntity entity;


    /**
     * 渠道状态枚举值
     */
    public enum ChannelStateEnum {
        CONFIRM_SUCCESS,    //接口正确返回：业务状态已经明确成功
        CONFIRM_FAIL,       //接口正确返回：业务状态已经明确失败
        WAITING,            //接口正确返回：上游处理中， 需通过定时查询/回调进行下一步处理
        API_RETURN_ERROR,   //渠道侧出现异常( 接口返回了异常状态 )
        SYS_ERROR,          //本系统出现不可预知的异常
        UNKNOWN             //接口超时，或网络异常等请求，或者返回结果的签名失败： 状态不明确 ( 上游接口变更, 暂时无法确定状态值 )
    }

    public ChannelReturnMessage(ChannelStateEnum channelStateEnum, String channelOrderId, String channelErrCode, String channelErrMsg) {
        this.channelStateEnum = channelStateEnum;
        this.channelOrderId = channelOrderId;
        this.channelErrCode = channelErrCode;
        this.channelErrMsg = channelErrMsg;
    }

    /**
     * 明确成功
     */
    public ChannelReturnMessage confirmSuccess(String channelOrderId) {
        return new ChannelReturnMessage(ChannelStateEnum.CONFIRM_SUCCESS, channelOrderId, null, null);
    }

    /**
     * 明确失败
     */
    public ChannelReturnMessage confirmSuccess() {
        return new ChannelReturnMessage(ChannelStateEnum.CONFIRM_FAIL, null, null, null);
    }

    /**
     * 明确失败
     */
    public ChannelReturnMessage confirmSuccess(String channelErrCode, String channelErrMsg) {
        return new ChannelReturnMessage(ChannelStateEnum.CONFIRM_FAIL, null, channelErrCode, channelErrMsg);
    }

    /**
     * 明确失败
     */
    public ChannelReturnMessage confirmSuccess(String channelOrderId, String channelErrCode, String channelErrMsg) {
        return new ChannelReturnMessage(ChannelStateEnum.CONFIRM_FAIL, channelOrderId, channelErrCode, channelErrMsg);
    }

    /**
     * 处理中
     */
    public ChannelReturnMessage waiting(String channelOrderId, String channelErrCode, String channelErrMsg) {
        return new ChannelReturnMessage(ChannelStateEnum.WAITING, channelOrderId, null, null);
    }


}
